<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class Configure extends Controller {
	
	function Configure()
	{
		parent::Controller();
		
		$this->load->library(array('form_validation', 'message'));
		$this->load->helper(array('form', 'date', 'language'));
		$this->load->model('settings');
		
		// Load language files
		$this->langs->fetch_file('configure');
    }
    
    function index()
    {
		// Temp
		$this->list_all();
    }
    
    function list_all()
    {
    	// Get stored settings
		$settings = $this->settings->get_all();
		
		$langs = $this->langs->get_installed();
		
		// Set validation rules
		$this->form_validation->set_rules('site_name', lang('configure_site_name'), 'trim|required');
		$this->form_validation->set_rules('site_description', lang('configure_site_description'), 'trim|required');
		$this->form_validation->set_rules('site_metadata', lang('configure_site_metadata'), 'trim|required');
		$this->form_validation->set_rules('site_lang', lang('configure_site_lang'), 'required');
		
		// Perform validation
		if ($this->input->post('save'))
		{	
			if ($this->form_validation->run())
			{
				// Try to save settings
				if ($this->settings->save())
				{
					$this->message->set('success', 'Ok, data saved');
				}
				else 
				{
					$this->message->set('error', 'Problem saving data');
				}
				
				redirect('admin/configure');
			}
			else
			{
				$this->message->set('error',  validation_errors(), TRUE);
			}
		}
		else if ($this->input->post('cancel'))
		{
			redirect('admin/configure');
		}
		
		$data['settings'] = $settings;
		$data['langs'] = $langs;
		
		$this->template->set_template('admin/main');
		$this->template->assign('langbar', $langs, 'admin/langbar');
		$this->template->assign('content', $data, 'admin/settings-form');
		$this->template->render();
	}
	
}
/* End of file configure.php */
/* Location: ./application/controllers/admin/configure.php */